package birdry.utils	{
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class BitmapUtil	{
		
		public static function returnScaleCrop( _obj: DisplayObject, _width:int, _height:int): BitmapData	{
			var per:Number = (_width / _obj.width > _height / _obj.height) ? _width / _obj.width : _height / _obj.height;
			var bd1:BitmapData = new BitmapData(_obj.width*per, _obj.height*per);
			bd1.draw(_obj, new Matrix( per, 0, 0, per, 0, 0));
			var bd2:BitmapData = new BitmapData(_width, _height);
			BitmapData(bd2).copyPixels(bd1, new Rectangle( (bd1.width - _width) / 2, (bd1.height - _height) / 2, _width, _height), new Point(0, 0));
			bd1.dispose();
			return bd2;
		}
		
		/**
		 * DisplayObject 객체를 BitmapData로 반환하여준다.
		 * @param	displayObject
		 * @param	transparent
		 * @param	toWidth
		 * @param	toHeight
		 * @param	startX
		 * @param	startY
		 * @param	fillColor
		 * @return
		 */
		public static  function returnBitmapData( displayObject: DisplayObject, transparent:Boolean = false, toWidth:int = 0, toHeight:int = 0, startX:uint = 0, startY:uint = 0, fillColor:uint = 0x000000): BitmapData	{
			if (toWidth == 0 || toHeight == 0)	{
				toWidth = displayObject.width;
				toHeight = displayObject.height;
			}
			var bitmapData:BitmapData = new BitmapData(toWidth, toHeight, transparent, fillColor);
			bitmapData.draw(displayObject, new Matrix( toWidth / displayObject.width, 0, 0, toHeight / displayObject.height, startX, startY));
			return bitmapData;
		}
		
		public static function returnCrop( obj:DisplayObject, x:Number, y:Number, width:Number, height:Number,  transparent:Boolean = false, fillColor:uint = 0x000000): BitmapData	{
			var bd:BitmapData = new BitmapData(obj.width, obj.height, transparent, fillColor);
			bd.draw(obj, new Matrix(1, 0, 0, 1, -x, -y), null, null, new Rectangle(0, 0, width, height));
			return bd;
		}
		
		public static function returnSplit( obj:DisplayObject, width:uint, height:uint ): Array	{
			
			var bitmapArr:Array = new Array();
			var bitmapData:BitmapData = BitmapUtil.returnBitmapData(obj);
			var colTotal:uint = Math.ceil(obj.width / width);
			var rowTotal:uint = Math.ceil(obj.height / height);
			
			for (var i:uint = 0; i < colTotal; ++i)	{
				bitmapArr[i] = new Array();
				for (var u:uint = 0; u < rowTotal; ++u)	{
					var tempBD:BitmapData = new BitmapData(width, height);
					tempBD.copyPixels( bitmapData, new Rectangle(i * width, u * height, (i * width) + width, (u * height) + height), new Point(0, 0) );
					bitmapArr[i].push(tempBD);
				}
			}
			bitmapData.dispose();
			return bitmapArr;
		}
		
		public static function returnMosaic( obj: DisplayObject, dotScale:uint = 10  ): Bitmap	{
			var bmd:BitmapData = new BitmapData(obj.width * (1/dotScale), obj.height * (1/dotScale));
			var matrix:Matrix = new Matrix();
			matrix.scale((1/dotScale), (1/dotScale));
			bmd.draw(obj, matrix);
			var bm:Bitmap = new Bitmap(bmd);
			bm.width = obj.width;
			bm.height = obj.height;
			return bm;
		}
		
		
		public static function returnBitmap( obj:DisplayObject, transparent:Boolean = true, fillColor:uint = 0x000000, smoothing:Boolean=false ): Bitmap	{
			var bitmapData:BitmapData = new BitmapData(obj.width, obj.height, transparent, fillColor);
			bitmapData.draw(obj, null, null, null, null, smoothing);
			var bitmap:Bitmap = new Bitmap(bitmapData);
			return bitmap;
		}
	}	// class
}	// package